#include "stdafx.h"
#include "TeamManager.h"

TeamManager::TeamManager()
{
}
TeamManager::~TeamManager()
{

}
Team* TeamManager::Add(TeamId id)
{
	Team* pTeam = manager<Team,TeamId>::Add(id);
	return pTeam;
}
bool TeamManager::Remove(TeamId id)
{
	return manager<Team,TeamId>::Remove(id);
}
TeamId TeamManager::createTeam(PlayerId stPlayerId)
{
	Player* pPlayer = singleton<PlayerManager>::instance().Get(stPlayerId);
	if (pPlayer == NULL)
		return 0;
	if(pPlayer->isInTeam())
	{
		LOGGER_ERROR("createTeam,pPlayer->isInTeam()");
		return 0;
	}
	Int32 nTeamId = GenerateIdInt32();
	Team* pTeam = Add(nTeamId);
	if (pTeam == NULL)
	{
		LOGGER_ERROR("createTeam,pTeam == NULL,nTeamId:%d",nTeamId);
		return 0;
	}
	pTeam->addTeamMember(stPlayerId);
	pTeam->setTeamLeader(stPlayerId);
	return nTeamId;
}
void TeamManager::destroyTeam(PlayerId stPlayerId)
{
	Player* pPlayer = singleton<PlayerManager>::instance().Get(stPlayerId);
	if (pPlayer == NULL)
		return;
	Team* pTeam = singleton<TeamManager>::instance().Get(pPlayer->GetTeamId());
	if (pTeam == NULL)
		return;
	TeamId nTeamId = pPlayer->GetTeamId();
	pTeam->clearTeam();
	Remove(nTeamId);
}
